package cn.wangkai.peanut.db.mod;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;

import cn.wangkai.peanut.commons.beanutils.BigDecimalConverter;
import cn.wangkai.peanut.commons.beanutils.DateConverter;
import cn.wangkai.peanut.commons.beanutils.SetConverter;
import cn.wangkai.peanut.commons.beanutils.TimestampConverter;
import cn.wangkai.peanut.db.util.ManagerException;

public class ModelUtil {
	
	public static <T> T put(T t,Map<String,Object> maps) throws IllegalAccessException,InvocationTargetException, InstantiationException {
	    ConvertUtils.register(new DateConverter(), Date.class);
	    ConvertUtils.register(new DateConverter(), java.sql.Date.class);
	    ConvertUtils.register(new TimestampConverter(), Timestamp.class);
	    ConvertUtils.register(new BigDecimalConverter(), BigDecimal.class);
	    ConvertUtils.register(new SetConverter(), Set.class);
        BeanUtils.populate(t, maps);
		return t;
	}
	
	/**
	 * 判断字符是否是主键
	 * @param keywords
	 * @param fieldname
	 * @return
	 */
	public static boolean equalsIgnoreCaseKeyWord(String[] keywords,String fieldname){
		for (int i = 0; i < keywords.length; i++) {
			String keyword = keywords[i];
			if(StringUtils.equalsIgnoreCase(keyword,fieldname)) return true;
		}
		return false;
	}
	
	/**
	 * 根据名称得到get方法对象
	 * @param obj
	 * @param filed
	 * @return
	 * @throws ManagerException 
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static Method getClassMethod(Object obj,String filed) throws ManagerException{
		try{
			Method[] methods = obj.getClass().getMethods();
			for (int i = 0; i < methods.length; i++) {
				Method method = methods[i];
				if(StringUtils.equalsIgnoreCase("get"+filed,method.getName())){
					return method;
				}
			}
			return null;
		}catch (Exception e) {
			throw new ManagerException(e);
		}
	}

	/**
	 * 获取类名称,不含路径
	 * @param c
	 * @return
	 */
	public static String getClassName(Class<?> c){
		Field[] fields=c.getDeclaredFields(); 

		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			try {
				if(StringUtils.equalsIgnoreCase(field.getName(),"TABLENAME")&&field.getModifiers()==25){
					return String.valueOf(field.get(c.newInstance()));
				}
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InstantiationException e) {
			}
		}
		return c.getName().replaceAll(c.getPackage().getName()+".", "");
	}
	
	
	public static String[] getClassPrimaryKeys(Class<?> c){

		Field[] fields=c.getDeclaredFields(); 

		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			try {
				if(StringUtils.equalsIgnoreCase(field.getName(),"PRIMARYKEY")&&field.getModifiers()==25){
					return new String[]{String.valueOf(field.get(c.newInstance()))};
				}else if(StringUtils.equalsIgnoreCase(field.getName(),"PRIMARYKEYS")&&field.getModifiers()==25){
					return (String[]) field.get(c.newInstance());
				}
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InstantiationException e) {
			}
		}
		return null;
	}
	
	/**
	 * 得到是否自增长
	 * @param c
	 * @return
	 */
	public static boolean getClassKeywordAuto(Class<?> c){

		Field[] fields=c.getDeclaredFields(); 

		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			try {
				if(StringUtils.equalsIgnoreCase(field.getName(),"KEYWORDAUTO")&&field.getModifiers()==25){
					return field.getBoolean(c.newInstance());
				}
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InstantiationException e) {
			}
		}
		return false;
	}
}
